#include "stdafx.h"
#include "HEventReceiver.h"
#include "HGlobalManager.h"
#include "CUserInterface.h"
#include "HRoomManager.h"
#include "GameRoom.h"
#include "engine/base/CUITypes.h"
#include "engine/base/Interface/CUIInputInterface.h"
#include "HCameraController.h"

namespace OSLib
{
	template<> BombBox::EventReceiver* Singleton<BombBox::EventReceiver>::ms_Singleton = NULL;
}

namespace BombBox
{
	EventReceiver::EventReceiver(void)
	{
		m_bMouseButtonDown = false;
	}

	EventReceiver::~EventReceiver(void)
	{
		ms_Singleton = NULL;
	}

	void EventReceiver::initialize(void)
	{
		g_UI->subscribeEvent(L"lbuttondown", GUI::CUIEventSubscriberSlot(&EventReceiver::onMouseLButtonDown, this));
		g_UI->subscribeEvent(L"rbuttondown", GUI::CUIEventSubscriberSlot(&EventReceiver::onMouseRButtonDown, this));
		g_UI->subscribeEvent(L"lbuttonup", GUI::CUIEventSubscriberSlot(&EventReceiver::onMouseLButtonUp, this));
		g_UI->subscribeEvent(L"rbuttonup", GUI::CUIEventSubscriberSlot(&EventReceiver::onMouseRButtonUp, this));
		g_UI->subscribeEvent(L"mousewheel", GUI::CUIEventSubscriberSlot(&EventReceiver::onMouseWheel, this));
		g_UI->subscribeEvent(L"mousemove", GUI::CUIEventSubscriberSlot(&EventReceiver::onMouseMove, this));
		g_UI->subscribeEvent(L"keydown", GUI::CUIEventSubscriberSlot(&EventReceiver::onKeyDown, this));
		g_UI->subscribeEvent(L"keyup", GUI::CUIEventSubscriberSlot(&EventReceiver::onKeyUp, this));

	}

	GUI::Bool EventReceiver::onMouseLButtonDown( const GUI::CUIInstructionBase& inst )
	{
		GUI::CUIInputInterface* l_input = GUI::CUIServiceProviderHelper::getInputDefault();
		if (l_input)
		{
			GUI::Vector2f pos = l_input->getMouseStatus().position;

			irr::core::line3d<irr::f32> ray;

			ray = s_pGlobalManager->getSceneCollisionManager()->getRayFromScreenCoordinates(
				irr::core::position2d<irr::s32>(pos.x,pos.y),s_pGlobalManager->getActiveCamera());

			s_pRoomManager->doPick(ray);
		}

		return true;
	}

	GUI::Bool EventReceiver::onMouseRButtonDown( const GUI::CUIInstructionBase& inst )
	{
		GUI::CUIInputInterface* l_input = GUI::CUIServiceProviderHelper::getInputDefault();
		if (l_input)
		{
			m_bMouseButtonDown = true;
		}

		return true;
	}

	GUI::Bool EventReceiver::onKeyUp( const GUI::CUIInstructionBase& inst )
	{
		const GUI::CUIINST_G2U_KEYUP* l_inst = dynamic_cast<const GUI::CUIINST_G2U_KEYUP*>( &inst );
		if (l_inst)
		{
			switch (l_inst->m_nKey)
			{
			case GUI::KEYMAP::K_EN_P:
				s_pRoomManager->play();
				break;
			case GUI::KEYMAP::K_EN_E:
				s_pRoomManager->edit();
				break;
			case GUI::KEYMAP::K_EN_N:
				s_pRoomManager->prev();
				break;
			case GUI::KEYMAP::K_EN_M:
				s_pRoomManager->next();
				break;
			//case GUI::KEYMAP::K_EN_R:
			//	s_pRoomManager->replay();
			//	break;
			case GUI::KEYMAP::K_EN_C:
				s_pRoomManager->create();
				break;
			case GUI::KEYMAP::K_EN_Y:
				s_pRoomManager->publish();
				break;
			case GUI::KEYMAP::K_EN_S:
				s_pRoomManager->save();
				break;
			case GUI::KEYMAP::K_EN_R:
				s_pRoomManager->load();
				break;
			}
		}

		return true;
	}

	GUI::Bool EventReceiver::onKeyDown( const GUI::CUIInstructionBase& inst )
	{
		const GUI::CUIINST_G2U_KEYDOWN* l_inst = dynamic_cast<const GUI::CUIINST_G2U_KEYDOWN*>( &inst );
		if (l_inst)
		{
		}

		return true;
	}

	GUI::Bool EventReceiver::onMouseLButtonUp( const GUI::CUIInstructionBase& inst )
	{
		GUI::CUIInputInterface* l_input = GUI::CUIServiceProviderHelper::getInputDefault();
		if (l_input)
		{
		}

		return true;
	}

	GUI::Bool EventReceiver::onMouseRButtonUp( const GUI::CUIInstructionBase& inst )
	{
		GUI::CUIInputInterface* l_input = GUI::CUIServiceProviderHelper::getInputDefault();
		if (l_input)
		{
			m_bMouseButtonDown = false;
		}

		return true;
	}

	GUI::Bool EventReceiver::onMouseWheel( const GUI::CUIInstructionBase& inst )
	{
		GUI::CUIInputInterface* l_input = GUI::CUIServiceProviderHelper::getInputDefault();
		if (l_input)
		{
			GUI::s16 delta = l_input->getMouseStatus().delta;

			s_pCameraController->updateCameraDist( delta * 0.04f );
		}

		return true;
	}

	GUI::Bool EventReceiver::onMouseMove( const GUI::CUIInstructionBase& inst )
	{
		GUI::CUIInputInterface* l_input = GUI::CUIServiceProviderHelper::getInputDefault();
		if (l_input && m_bMouseButtonDown)
		{
			GUI::Vector2f delta = l_input->getMouseStatus().positionChange;
			s_pCameraController->updateCameraRotate( irr::core::vector2df(delta.x, 0) );
		}
		return true;
	}
}